/**
 * 
 */
package research.sbookmarking.input;

import research.sbookmarking.*;
import research.sbookmarking.process.*;
import research.sbookmarking.output.*;
import org.afox.util.*;
import org.connotea.*;
import java.util.*;

/**
 * This is inherits {@link Command Command} class. This class deals with particular
 * URL, userName, and date queries.
 * 
 * <p> 
 * @author <A HREF="mailto:aatleung@gmail.com">Albert Leung</a>
 * @version 1.0
 * 
 */
public class BookmarkCommand extends Command {

	public BookmarkCommand(String queryName, List command) {
		super(queryName, command);
}

	public BookmarkCommand(String queryName, List command, Integer limit,Integer start) {
		super(queryName, command, limit, start);
	}

	/* (non-Javadoc)
	 * @see research.sbookmarking.input.Command#execute()
	 */
	public void execute() {
		//getList of bookmarks
		ArrayList results = new ArrayList();
		ArrayList parameters = new ArrayList();
		GeneralContainer container =new GeneralContainer(getQueryName());
		Connotea database = new Connotea("alby1976","a1014leung");

		Iterator items = getCommands().iterator();
		while (items.hasNext()){

			parameters.add((String)items.next());
			retrieveData(database, container, parameters, results);
			parameters.clear();
		}
		if (this.isSaveResults()) {
			container.setQueryResults(results);
			setResult(container);
		}
		else {
			setResult(container);
		}
	}

	protected void retrieveData (Connotea database, DataContainer container, List param, ArrayList results) {
		ArrayList subset = new ArrayList();
		ArrayList temp = new ArrayList();
		
		Object obj = RMethod.invoke(this, "process"+getQueryName(), param);
		getAll (subset, database, "getPosts",obj, getLimit(),getStart());

		String filename = "data/" + param.get(0)+"-bookmarks-all.rtf.gz";
		temp.add(new GeneralContainer(getQueryName(),subset));
		OutputController.getInstance().saveRawData(filename, temp);

		if (this.isSaveResults()){
			copy(results,subset.iterator());
		}
		else {
			results = temp;
		}
		container.setQueryResults(results);
		ProcessController.getInstance().processData(container);
	}
/*	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			Connotea database = new Connotea("alby1976","a1014leung");
			//Reference ref = new Reference("http://www.dlib.org/dlib/april05/lund/04lund.html");
			//System.out.println ("ref: "+ref);
			//List obj=(List)database.getPosts(ref,10,0);
			//System.out.println(obj);
			
			ArrayList results = new ArrayList();
			//Calendar date = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
			Calendar date = Calendar.getInstance();
			//date.set(2008, Calendar.JUNE, 13,0,0);
			date.set(2008, Calendar.JUNE, 13);
			BookmarkCommand bookmark = new BookmarkCommand(null,null); 
			bookmark.getAll (results, database, "getPosts",date.getTime(), 10,0);

/*
			obj = (List)database.getPosts(,10,0);
			System.out.println("0: "+obj);
			obj = (List)database.getPosts(date.getTime(),10,10);
			System.out.println("10: "+obj);
			obj = (List)database.getPosts(date.getTime(),10,20);
			System.out.println("20: "+obj);
			obj = (List)database.getPosts(date.getTime(),10,30);
			System.out.println("30: "+obj);
			obj = (List)database.getPosts(date.getTime(),10,40);
			System.out.println("40: "+obj);
			//System.out.println(database.getBookmarks((Reference)aBookmark.getLink()));

			//String command = new String ("getBookmarks");
			//ArrayList param = new ArrayList();
			
			//param.add("http://www.connotea.org/data/uri/685b90ae66cfbc3fc8ebeed0a5def571");
			//System.out.println("method: "+ command+" param:"+param);
			//Object objs = (RMethod.invoke(database,command,param));
			//System.out.println(objs);
			//System.out.println("URL: " + ((Bookmark)((List)result.get(0)).get(0)).getLink());

		}
		catch (Exception e){
			System.out.println(e + ":" + e.getMessage());
			System.out.println(e + ":" + e.getCause());

		}
	}
*/
}
